//package com.demo.interceptor;
//
//import com.tal.mathat.entity.OpenServerApp;
//import com.tal.mathat.exception.ErrorCode;
//import com.tal.mathat.service.OpenServerAppService;
//import com.tal.mathat.utils.EmptyHelper;
//import com.tal.mathat.utils.JWTUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Map;
//
//@Component
//public class JWTInterceptor implements HandlerInterceptor {
//
//    @Autowired
//    private OpenServerAppService openServerAppService;
//
//    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
//                             Object object) {
//        try {
//            String token = httpServletRequest.getHeader("JWT");
//
//            if (token == null) {
//                refuseResult(httpServletResponse);
//                return false;
//            }
//            if (!JWTUtils.verify(token)) {
//                refuseResult(httpServletResponse);
//                return false;
//            }
//            Map<String, Object> contentMap = JWTUtils.getContentMap(token);
//            String appId = contentMap.containsKey("appId") ? (String) contentMap.get("appId") : "";
//            if (StringUtils.isBlank(appId)) {
//                refuseResult(httpServletResponse, ErrorCode.SystemError.ERROR_APPID_NOT_CREATE.getCode()
//                        ,ErrorCode.SystemError.ERROR_APPID_NOT_CREATE.getMessage());
//                return false;
//            }
//            OpenServerApp openServerApp = openServerAppService.getServerAppByAppId(appId);
//
//            if (EmptyHelper.isEmpty(openServerApp)) {
//                refuseResult(httpServletResponse, ErrorCode.SystemError.ERROR_APPID_NOT_CREATE.getCode(),
//                        ErrorCode.SystemError.ERROR_APPID_NOT_CREATE.getMessage());
//                return false;
//            }
//            if (openServerApp.getStatus() == 0) {
//                refuseResult(httpServletResponse, ErrorCode.SystemError.ERROR_APPID_DISABLED.getCode(),
//                        ErrorCode.SystemError.ERROR_APPID_DISABLED.getMessage());
//                return false;
//            }
//
//            // YJXY 粤教翔云 长期token 类型校验
//            if ("YJXY".equals(openServerApp.getAppName())) {
//                String tokenType = contentMap.containsKey("type") ? (String) contentMap.get("type") : "";
//                if (StringUtils.isNotBlank(tokenType) && !"long".equals(tokenType)) {
//                    return false;
//                }
//            }
//
//            return true;
//        } catch (Exception e) {
//            try {
//
//                refuseResult(httpServletResponse);
//                return false;
//            } catch (Exception exception) {
//                return false;
//            }
//        }
//    }
//
//    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o,
//                           ModelAndView modelAndView) throws Exception {
//
//    }
//
//    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
//                                Object o, Exception e) throws Exception {
//
//    }
//
//    public void refuseResult(HttpServletResponse httpServletResponse) {
//        try {
//            httpServletResponse.setContentType("text/html;charset=UTF-8");
//            httpServletResponse.setCharacterEncoding("UTF-8");
//            httpServletResponse.setStatus(200);
//            httpServletResponse.getWriter().print("{\"code\":2001,\"message\":\"登录信息有误或已失效,请重新登录\"}");
//            httpServletResponse.getWriter().flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    public void refuseResult(HttpServletResponse httpServletResponse,  Integer code,String message) {
//        try {
//            httpServletResponse.setContentType("text/html;charset=UTF-8");
//            httpServletResponse.setCharacterEncoding("UTF-8");
//            httpServletResponse.setStatus(200);
//            httpServletResponse.getWriter().print("{\"code\":"+code+",\"message\":\"" + message + "\"}");
//            httpServletResponse.getWriter().flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//}
